package com.briup.smartlabs.web.controller;

import com.briup.smartlabs.bean.ex.Device;
import com.briup.smartlabs.bean.ex.DeviceType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.briup.smartlabs.common.utils.Response;
import com.briup.smartlabs.service.SmartDeviceService;
import com.briup.smartlabs.web.vm.CommandVM;
import com.github.pagehelper.PageInfo;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;
import java.util.Map;

@Api(tags = "设备服务")
@RestController
@RequestMapping("/device")
public class DeviceController {
	@Autowired
	private SmartDeviceService deviceService;

	@GetMapping("/by/condition")
	public Response<PageInfo<Device>> findByCondition(
			int pageNum,
			int pageSize,
			String typeId, String name) {
		PageInfo<Device> byCondition = deviceService.findByCondition(pageNum, pageSize, typeId, name);
		return Response.ok(byCondition);
	}

	@ApiOperation(value = "发送指令，控制设备运行")
	@PostMapping("/sendCommand")
	public Response<String> sendCommand(CommandVM command) {
		deviceService.sendMessage(command.toString());
		return Response.ok("操作成功！");
	}


	@DeleteMapping("{id}")
	@ApiOperation("根据id删除")
	public Response<String> deleteById(@PathVariable("id") String id) {
		deviceService.removeById(id);
		return Response.ok("删除设备成功!");
	}

	@DeleteMapping("/batch")
	@ApiOperation("批量删除")
	public Response<Map<String, List<String>>> deleteByBatch(String[] ids) {
		Map<String, List<String>> map = deviceService.removeByBatch(ids);
		return Response.ok(map);
	}


	@PostMapping("/saveOrUpdate")
	@ApiOperation("更新和添加")
	public Response<String> saveOrUpdate(@RequestBody Device device) {
		deviceService.saveOrUpdate(device);
		return Response.ok("操作成功");
	}

}





